<h2>Basics</h2>
<p>
	Basic DomAr class looks something like this.
</p>
<p>
	DomAr is built in a way that it automatically creates cache for your class files and checks if file is modified. (Can be turned off to boost performance when project is finished.)
	Notice that Dom library automatically maintains class file paths and scans dirs to find your classes and uses autoload to require them if needed. 
	You should never need to require or include any of your class files.
</p>
<p>
	NB! NEVER require your DomArObject classes as original files as they should never be loaded into your scripts! There will be class name conflict!
</p>
{literal}
<pre class="code">
&lt;?php
/**
* @orm tableName sampleclass
*/
class MySampleClass extends DomArObject {
	/**
	* @orm text
	*/
	protected $name;
	/**
	* @orm int(3)
	*/
	public $age;
	
	protected $nonRecordProperty;
	
	public function MyMethod() {}
}
?&gt;
</pre>
{/literal}
<p>NB! All files that contain DomAr class must be named SOME_NAME.domar.php</p>


<h2>Compiler</h2>

<p>
All DomAr classes are compiled. This means that your original file will not be used. 
Your class will be rewriten and new methods will be added.
</p>

<p>
This is something similar to compiled class.
</p>

{literal}
<pre class="code">
&lt;?php
class MySampleClassRecord extends DomArObject {
	
	private $name;

	private $age;
}
class MySampleClass extends MySampleClassRecord {
	
	protected $nonRecordProperty;
	
	public function MyMethod() {}
}
?&gt;
</pre>
{/literal}

{literal}
<h2>Using</h2>

Saving object to database
<pre class="code">
&lt;?php
// This should be called only once! It creates database table for object.
// Call this method also when you make changes to your model. (NB! Always backup your data!!!)
MySampleClass::getTable()->update();

$item = new MySampleClass();
$item->name = 'My sample name';
$item->age = 10;

//SAVING
$item->save();

// LOADING
$item = MySampleClass::load( $id );
// loading multiple objects
$items = MySampleClass::query()->where("name LIKE 'My sample name'")->commit();

// DELETING
$item->delete();
?&gt;
</pre>
{/literal}

















<h2>First relations</h2>
<p>
	Relations are also mapped trough annotations. There are two types of relations: aggregation and composition. Main difference between them is that 
	composition owns their children and if parent is deleted so will be the children. Aggregation on the other hand will not delete its children.
</p>
<p>
	Inversing makes managing related objects really easy as every object knows who are his parents and who are his children. 
</p>

	When you add a child to parent, then the parent will be automatically added	to child.
{literal}
<pre class="code">
&lt;?php
$parent = new MyParent();
$child = new MyChild();

$parent->child  = $child;
// this is true
if( $child->parent === $parent ) 
	echo 'TRUE';

$parent->child = NULL; // this also deletes $child from database as its composition!
// this is true
if( $child->parent === NULL ) 
	echo 'TRUE';
?&gt;
</pre>
{/literal}

		
	
	
	
		
		
{literal}	
<h2> Simple 0ne to one relation </h2>
<h3>Source</h3>
<pre class="code">
&lt;?php
class MyParent extends DomArObject {
	/**
	* @orm owns child MyChild inverse parent 
	*/
	private $child;		
}
			
class MyChild extends DomArObject {				
	/**
	* @orm has parent MyParent inverse child
	*/
	private $parent;			
}
?&gt;
</pre>
<h3>Using</h3>
<pre class="code">
&lt;?php
$parent = new MyParent();
$parent->child = new MyChild();

// saving parent and child
$parent->save();

//Accessing child
$parent = MyParent::load( $id );
$child = $parent->child;

//Accessing parent through child
$child = MyChild::load( $id );
$parent = $child->parent;

//Deleting parent and child
$parent->delete();
?&gt;
</pre>
		
		
		
		
		
		
		
		
		
		
		
		
		
<h2> Simple 0ne to many relation </h2>
<h3>Source</h3>
<pre class="code">
&lt;?php
class MyParent extends DomArObject {
	/**
	* @orm owns children MyChild inverse parent 
	*/
	private $children;		
}
			
class MyChild extends DomArObject {				
	/**
	* @orm has parent MyParent inverse children
	*/
	private $parent;			
}
?&gt;
</pre>
<h3>Using</h3>
<pre class="code">
&lt;?php
$parent = new MyParent();
$parent->children[] = new MyChild();
$parent->children[] = new MyChild();
$parent->save();

//Accessing children

$parent = MyParent::load( $id );
foreach( $parent->children as $child ) {
	// do something
}

// access children by some criteria
foreach( $parent->children->query()->where('t.id>10')->limit(5)->orderBy('t.id')->commit() as $child ) {
	// do something
}

//Deleting parent and children
$parent->delete();
?&gt;
</pre>

		
		
		
		
		
		
		
		
		
		

<h2> Simple Many to many relation </h2>
<h3>Source</h3>
<pre class="code">
&lt;?php
class MyParent extends DomArObject {
	/**
	* @orm has children MyChild inverse parents 
	*/
	private $children;		
}
			
class MyChild extends DomArObject {				
	/**
	* @orm has parents MyParent inverse children
	*/
	private $parents;			
}
?&gt;
</pre>
<h3>Using</h3>
<pre class="code">
&lt;?php
$parent1 = new MyParent();
$parent2 = new MyParent();
$child1 = new MyChild();
$child2 = new MyChild();

$parent1->children[] = $child1;
$parent1->children[] = $child2;

$parent2->children[] = $child1;

$parent->save();
	
//Accessing children and parents

$parent = MyParent::load( $parent1->id );
foreach( $parent->children as $child ) {
	// do something
}

$child = MyChild::load( $child1->id );
foreach( $child->parents as $parent ) {
	// do something
}
?&gt;
</pre>
		
		
<h2>Listeners</h2>	
<p>
DomAr has very cool listener system built in. With that you can make your model even better! See more about listeners in listeners section.
Basicly listeners are methods that are called on specific events. Some of them are beforeLoad, beforeSave, beforeUpdate, beforeInsert, beforeAdd (On 1:N or N:M relationships)
and afterLoad, afterSave, afterUpdate, afterInsert, afterAdd... <br>
<br>
</p>

<h2>Validators</h2>	
<p>
Building forms where data has to be validated? <br>
Worried about that text size is longer than 10 or some numbers are only between 10 and 20. <br>
Well see about validators, they will make youre life super easy.<br>
</p>
<pre class="code">
&lt;?php
class MyClass extends DomArObject {

	/**
	* @orm number(2)
	* @orm validator method myListenerMethod
	*/
	public $someNumber;

	public function myListenerMethod( $value ) {
		if( $value > 10 )
			return 1001;
		else if( $value < 0 )
			return 1002;
		else
			return 0;
	}
	
	public function getValidationErrorMessages() {
		return array(
			'someNumber'  => array(
				1001 => 'Your number must be smaller than 10',
				1002 => 'Your number must be bigger than 0',
			);
		);
	}
}

// usage sample
$item = new MyClass();
$item->someNumber = 11;
$item->someNumber = -1;
if( $item->save() ) {
	// everything is cool, object saved.
} else {
	// OPTION 1
	// there were some validation errors
	$errors = $item->getValidationReport()->messages;
	echo implode('&lt;br&gt;', $errors ); // prints out all errors.
	// for our example it will print "Your number must be smaller than 10&lt;br&gt;Your number must be bigger than 0"
	
	// OPTION 2
	// get only last error
	echo $item->getLastValidationMessage(); // echoes "Your number must be bigger than 0"
}
?&gt;
</pre>
		
{/literal}